﻿CREATE PROCEDURE [dbo].[friends_set]
    @userId int,
    @friendId int 

    AS

    DECLARE @Count int =0

    SELECT @Count = COUNT(1) FROM Friends 
    WHERE Friends.UserId = @friendId and Friends.FriendId = @userId 
    IF @Count!=0 
    BEGIN
        UPDATE Friends SET Friends.IsAccepted = 1
        WHERE Friends.FriendId = @userId and Friends.UserId = @friendId 
        INSERT INTO Friends
        (
            UserId,
            FriendId,
            IsAccepted
        )
        VALUES
        (
            @userId,
            @friendId,
            1
        )
    END
    ELSE
    BEGIN
        INSERT INTO Friends
        (
            UserId,
            FriendId,
            IsAccepted
        )
        VALUES
        (
            @userId,
            @friendId,
            0
        )
    END
